package com.youjia.center.boot.data.mp.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * MP自动填充
 * 判断localDateTime Date
 *
 * @author fengjiansong
 * @date 2022/1/18 11:09 下午
 */
@Component
public class AutoModifyDataHandler implements MetaObjectHandler {

    protected Log log = LogFactory.getLog(getClass());

    /**
     * localDateTime
     */
    private final String LOCAL_DATE_TIME = "java.time.LocalDateTime";

    /**
     * date
     */
    private final String DATE = "java.util.Date";

    /**
     * 新增handler
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {

        Class<?> createTimeType = metaObject.getGetterType("createTime");

        // 自动填充date
        if (DATE.equals(createTimeType.getTypeName())) {
            log.debug("mp insert fill createTime");
            this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        }

        // 自动填充localDateTime
        if (LOCAL_DATE_TIME.equals(createTimeType.getTypeName())) {
            log.debug("mp insert fill localDateTime");
            this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class);
        }

        // 自动填充新增人createBy
        Object createById = metaObject.getValue("createBy");
        if (createById != null) {
            log.debug("mp insert fill createBy");
            this.strictInsertFill(metaObject, "createBy", createById::toString, String.class);
        }

        // 自动填充逻辑删除标志位
        log.debug("mp insert fill deleteFlag");
        this.setFieldValByName("deleteFlag", false, metaObject);

        // 自动填充version
        if(metaObject.hasGetter("lockVersion")){
            log.debug("mp insert fill version");
            this.setFieldValByName("lockVersion", 0L, metaObject);
        }
    }

    /**
     * 更新handler
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {

        Class<?> createTimeType = metaObject.getGetterType("updateTime");

        if (DATE.equals(createTimeType.getTypeName())) {
            log.debug("mp update fill Date");
            this.setFieldValByName("updateTime", new Date(), metaObject);
        }
        if (LOCAL_DATE_TIME.equals(createTimeType.getTypeName())) {
            log.debug("mp update fill localDateTime");
            this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        }
        Object updateById = metaObject.getValue("updateBy");
        if (updateById != null) {
            log.debug("mp update fill createBy");
            this.strictInsertFill(metaObject, "updateBy", updateById::toString, String.class);
        }
        Object createById = metaObject.getValue("createBy");
        if (createById != null) {
            log.debug("mp update fill createBy");
            this.strictInsertFill(metaObject, "updateBy", createById::toString, String.class);
        }
    }

}